package com.crb.crm.manage.excel.listener;

import com.crb.crm.manage.dao.biz.SystemConfigInfoMapper;
import com.crb.crm.manage.entity.biz.SystemConfigDTO;
import com.crb.crm.manage.excel.ExcelTemplate;
import com.crb.crm.manage.excel.model.SystemConfigModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Package: com.crb.crm.manage.excel
 * Description：
 * Author: huangjian
 * Date:  2020/9/4 17:07
 * Modified By:
 *
 * @author huangjian
 */
@Slf4j
@Component
class SystemConfigExcelListener extends BaseExcelListener<SystemConfigModel> {

    @Resource
    private SystemConfigInfoMapper infoMapper;

    @Override
    protected void validData(SystemConfigModel systemConfigModel, ValidResult validResult) {
        if (StringUtils.isBlank(systemConfigModel.getCode())) {
            validResult.addError("编码不能为空");
        }else{
            SystemConfigDTO oneSystemConfigByCode = infoMapper.getOneSystemConfigByCode(systemConfigModel.getCode());
            if(Objects.nonNull(oneSystemConfigByCode)){
                validResult.addError("编码已存在");
            }
        }
        if(systemConfigModel.getContent().contains("5")){
            validResult.addError("版本号不能包括5");
        }

    }

    @Override
    public void saveData(List<SystemConfigModel> list) {
        infoMapper.saveBatch(list);
    }

    @Override
    public ExcelTemplate support() {
        return ExcelTemplate.SYSTEM_CONFIG;
    }
}
